/**
 *	流程模块主Flow录入页使
 *	开发者：雨中磐石
 *	地址：www.rockoa.com
 *	日期：2023-07-19
 */

package com.framework.Flow;



import com.framerock.baselib.Json;
import com.framerock.baselib.Rock;
import com.framework.ACore.Model;
import com.framework.Model.ModelOption;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class FlowStore extends FlowBase {

    private Map<String, ArrayList<Map<String, String>>> allStoreObj;

    public FlowStore(Flow flows) {
        super(flows);
        allStoreObj = new HashMap<String, ArrayList<Map<String, String>>>();
    }


    public String getStore(String zdid)
    {
        Map<String, String> rs = Model.get(this.nDb, "flowelement").getOne("`id`="+zdid+" and `modeid`="+mFlow.modeid+"");
        if(rs==null)return "";
        String data = Rock.getMapString(rs, "data");
        return getStore(data, rs.get("fieldstype"), rs.get("fields"));
    }

    public String getStore(String data, String fieldstype, String fields)
    {
    	if(fieldstype.equals("checkbox") && Rock.isEmpt(data))data = "0|否,1|是";
        if(Rock.isEmpt(data))return  "";
        ArrayList<Map<String, String>> lista = getallStoreList(fields);
        if(lista != null)return Json.getListString(lista);

        String action = "",arr[] = data.split(","),name,value,dstr="",arr1[];
        int len = arr.length;
        action  = arr[0]; //请求的方法
        ModelOption option = null;
        if(Rock.contain(data, ",,")){ //,,flowset|id,name|id>0直接读取表

        }else if(Rock.contain(action,":")) {//Flow下的Store
            arr1    = action.split(":");
            if(len>1)dstr = arr[1];
            lista   = Flow.gets(this.nDb, arr1[0], mRequest).Store(arr1[1], dstr);
        }else if(fieldstype.equals("rockcombo")) {
            if(option==null)option = ModelOption.get(this.nDb);
            value = "name";
            if(len>1)value = arr[1];
            if(mFlow.isCompanymode())action+="_"+mRequest.COMPANYID+"";
            if(len>2 && arr[2].equals("tree")){
                lista = option.getTreeData(action, "",value);
            }else {
                lista = option.getData(action, value);
            }
            if(lista == null)lista = mFlow.Store(action, "");
        }else{
            lista   = mFlow.Store(action, "");
            if(lista == null) {
                lista = Rock.getMapList();
                for (int i = 0; i < arr.length; i++) {
                    arr1 = arr[i].split("\\|");
                    name = arr1[0];
                    value = arr1[0];
                    if (arr1.length > 1) name = arr1[1];
                    dstr = "name=" + name + ",value=" + value + "";
                    if (arr1.length > 2) dstr += ",color=" + arr1[2] + "";
                    lista.add(Rock.getMap(dstr));
                }
            }
        }
        if(lista==null)lista = Rock.getMapList();
        allStoreObj.put(fields, lista);
        return Json.getListString(lista);
    }


    /**
     * 获取所有的数据源checkboxall,select,rockcombo,radio
     * lx=0默认,1表单页,2详情页
     * */
    public String getallStore(int lx)
    {
        Json json = new Json();
        ArrayList<Map<String, String>> farr = mFlow.getFieldsArr(0);
        if(farr==null)return json.getJson();
        String type,data;
        Map<String, String> da;
        String varr[] = new String[]{"checkboxall","select","rockcombo","radio", "not"};
        if(lx==2)varr[4] = "checkbox";
        for(int i=0;i<farr.size();i++){
            da = farr.get(i);
            type = da.get("fieldstype");
            if(
                    (lx==0) ||
                    (lx==2) ||
                    (lx==1 && da.get("islu").equals("1"))
            ){
                if(Rock.inArray(varr, type)) {
                    data = this.getStore(da.get("data"), type, da.get("fields"));
                    if(data.length()==0)data = "[]";
                    json.putVal(da.get("fields"), data);
                }
            }
        }
        return  json.getJson();
    }

    /**
     * 所有字段数据源
     * */
    public ArrayList<Map<String, String>> getallStoreList(String fields)
    {
        ArrayList<Map<String, String>> list = allStoreObj.get(fields);
        return list;
    }
}
